package short_semester.questionnaire_planet.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import short_semester.questionnaire_planet.request.editRequest.EditQuestionBody;

import javax.persistence.*;
import java.util.LinkedList;
import java.util.List;

@Entity
@Table
@Data
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
@NoArgsConstructor
public class SingleChoiceQuestion {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
    private Questionnaire questionnaire;
    

    @Column
    private String title = "";

    @Column
    private String description = "";

    // 是否必答
    @Column
    private Boolean isNecessary = true;

    @Column     //选项顺序
    @JsonIgnore
    private Integer sequence;
    
    @Column     //是否选项随机
    private Boolean isRandom = false;

    @Column
    private Integer count = 0;

    //子类型
    private String qtype = null;
    
    @Transient
    private String type = "radio";
    
    
    //考试特殊属性
    @Column
    private Integer grade = 0;
    
    @Column
    private String resolution = "";
    
    //投票单选特殊属性
    private Boolean isShowCount = false;
    private Boolean isShowPercent = false;
    

    //关联逻辑
    private String logic = "且";

    private String relationString;
    @Transient
    private Boolean isShow = true;
    
    @Transient
    private List<SingleChoiceOption> options = new LinkedList<>();
    
    public void setValueFrom(EditQuestionBody body){
        title = body.getTitle();
        description = body.getDescription();
        isNecessary = body.getIsNecessary();
        isRandom = body.getIsRandom();
        
        grade=body.getGrade();
        resolution=body.getResolution();
        
        isShowCount=body.getIsShowCount();
        isShowPercent=body.getIsShowPercent();

    }
    
    public SingleChoiceQuestion(SingleChoiceQuestion question){
        title = question.getTitle();
        description = question.getDescription();
        isNecessary = question.getIsNecessary();
        isRandom = question.getIsRandom();
        sequence = question.getSequence();
        
        qtype=question.getQtype();
        
        grade=question.getGrade();
        resolution=question.getResolution();
        
        isShowCount = question.getIsShowCount();
        isShowPercent = question.getIsShowPercent();

    }
    
    public SingleChoiceQuestion(String qtype){
        this.qtype=qtype;
    }
}
